﻿package com.urbansquall.ui
{

import flash.display.*;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;

public class RegularBitmapButton extends SimpleButton
{
	private var m_overThings: Array /*Sprite*/;
	
	public function RegularBitmapButton( a_img: DisplayObject, a_toggleImg: DisplayObject = null )
	{
		var ct:ColorTransform;
		m_overThings = [];
		
		var up:Bitmap = new Bitmap();
		up.bitmapData = new BitmapData( a_img.width, a_img.height, true, 0x00000000 );
		up.bitmapData.draw( a_img );
		var upCont:Sprite = new Sprite();
		upCont.addChild( up );
		this.upState = upCont;
		
		var over: Bitmap = new Bitmap();
		over.bitmapData = new BitmapData( a_img.width, a_img.height, true, 0x00000000 );
		ct = new ColorTransform( 1.3, 1.3, 1.3 );
		over.bitmapData.draw( a_img, null, ct );
		var overCont:Sprite = new Sprite();
		overCont.addChild( over );
		this.overState = overCont;
		
		var down: Bitmap = new Bitmap();
		down.bitmapData = new BitmapData( a_img.width, a_img.height, true, 0x00000000 );
		ct = new ColorTransform( 0.8, 0.8, 0.8 );
		down.bitmapData.draw( a_img, null, ct );
		var downCont:Sprite = new Sprite();
		downCont.addChild( down );
		this.downState = downCont;
		
		this.hitTestState = up;
		this.cacheAsBitmap = true;
		this.useHandCursor = true;
		
		if( a_toggleImg != null )
		{
			var upThing: Bitmap = new Bitmap();
			upThing.bitmapData = new BitmapData( a_toggleImg.width, a_toggleImg.height, true, 0x00000000 );
			upThing.bitmapData.draw( a_toggleImg );
			upCont.addChild( upThing );
			
			var overThing: Bitmap = new Bitmap();
			overThing.bitmapData = new BitmapData( a_toggleImg.width, a_toggleImg.height, true, 0x00000000 );
			overThing.bitmapData.draw( a_toggleImg );
			overCont.addChild( overThing );
			
			var downThing: Bitmap = new Bitmap();
			downThing.bitmapData = new BitmapData( a_toggleImg.width, a_toggleImg.height, true, 0x00000000 );
			downThing.bitmapData.draw( a_toggleImg );
			downCont.addChild( downThing );
			
			m_overThings = [ upThing, overThing, downThing ];
			toggleOverThings();
			
			this.addEventListener( MouseEvent.CLICK, onClick );
		}
	}
	
	private function onClick( a_event: MouseEvent ): void
	{
		toggleOverThings();
	}
	
	private function toggleOverThings(): void
	{
		for each( var t: Bitmap in m_overThings )
		{
			t.visible = ! t.visible;
		}
	}
}
	
}